<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../../Css/style.css" />
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>

 

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script>
    $(function () {
		$('#backid').click(function(){
				window.location.href="/pages/user/index.html";
		 });

		$.getJSON("/user/getroleinfo.do",null,function (vo){
		    let list=vo.list;
		    let str="<option value=\"0\">--"+"请选择"+"--\n";;
		    for(let i=0;i<list.length;i++){
		        str+="<option value=\""+list[i].rId+"\">--"+list[i].rName+"--\n";
            }
		    $("#role").html(str);
        });

    });

    function validataLoginname() {
        var name = $("#uLoginname").val();
        var msg = $("span[id='loginnameMsg']");
        $.getJSON("/user/getuserlist.do",null,function (vo){
            let userList=vo.list;
            for(let i=0;i<userList.length;i++){
                if(name==userList[i].uLoginname){
                    msg.html("用户名已存在").css("color", "red");
                    return false;
                }
            }
        });

        if(name == "") {
            msg.html("用户名不能为空").css("color", "red");
            return false;
        } else if(name.length < 6) {
            msg.html("用户名长度必须大于6位").css("color", "red");
            return false;
        } else {
            msg.html("ok").css("color", "green");
            return true;
        }
    }

    function validataPwd() {
        var pwd = $("#uPassword").val();
        var msg = $("#passMsg");
        if(pwd == "") {
            msg.html("密码不能为空").css("color", "red");
            return false;
        } else if(pwd.length < 8) {
            msg.html("密码长度必须大于8位").css("color", "red");
            return false;
        } else {
            msg.html("ok").css("color", "green");
            return true;
        }
    }

    function validataTruename() {
        var tname = $("#uTruename").val();
        var msg = $("#truenameMsg");
        if(tname == "") {
            msg.html("真实姓名不能为空").css("color", "red");
            return false;
        } else {
            msg.html("ok").css("color", "green");
            return true;
        }
    }


    function validataEmail() {
        var email = $("#uEmail").val();
        var msg = $("#emailMsg");
        var emailTest=/^\w{5,}@[a-z0-9]{2,3}\.[a-z]+$|\,$/;
        if(email == "") {
            msg.html("邮箱不能为空").css("color", "red");
            return false;
        } else if(!emailTest.test(email)){
            msg.html("邮箱格式不正确").css("color", "red");
            return false;
        }else {
            msg.html("ok").css("color", "green");
            return true;
        }
    }

    function validataState() {
        var all = $("input[name='status']");
        for(var i = 0; i < all.length; i++) {
            if($(all[i]).prop("checked")) {
                return true;
            }
        }
        alert("状态为必选项");
        return false;
    }

    function validataRole() {
        var ca = $("#role").val();
        if(ca == 0) {
            alert("角色为必选项");
            return false;
        } else {
            return true;
        }
    }

    $(function() {
        $("#uLoginname").blur(function() {
            validataLoginname();
        });

        $("#uPassword").blur(function() {
            validataPwd();
        });

        $("#uTruename").blur(function() {
            validataTruename();
        });

        $("#uEmail").blur(function() {
            validataEmail();
        });
    });

    function validataAdd() {
        if (validataLoginname() && validataPwd() && validataTruename() && validataEmail() &&validataState() &&validataRole()){
            let uLoginname=$("#uLoginname").val();
            let uPassword=$("#uPassword").val();
            let uTruename=$("#uTruename").val();
            let uEmail=$("#uEmail").val();
            let uState=$("input[name='status']:checked").val();
            let rId=$("#role option:selected").val();
            let user={
                uLoginname:uLoginname,
                uPassword:uPassword,
                uTruename:uTruename,
                uEmail:uEmail,
                uState:uState,
                rId:rId
            }
            $.getJSON("/user/add.do",user,function (vo){
                if (vo.code==200){
                    alert("添加成功");
                    window.location.href="/pages/user/index.html";
                }else {
                    alert(vo.msg);
                }
            });
        }
    }
</script>
</head>
<body>
<form action="" method="post" class="definewidth m20">
<input type="hidden" name="id" value="{$user.id}" />
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">登录名</td>
            <td><input id="uLoginname" type="text" name="username"/><span id="loginnameMsg"></span></td>
        </tr>
        <tr>
            <td class="tableleft">密码</td>
            <td><input id="uPassword" type="password" name="password"/><span id="passMsg"></span></td>
        </tr>
        <tr>
            <td class="tableleft">真实姓名</td>
            <td><input id="uTruename" type="text" name="realname"/><span id="truenameMsg"></span></td>
        </tr>
        <tr>
            <td class="tableleft">邮箱</td>
            <td><input id="uEmail" type="text" name="email"/><span id="emailMsg"></span></td>
        </tr>
        <tr>
            <td class="tableleft">状态</td>
            <td>
                <input type="radio" name="status"checked value="0"/> 启用
              <input type="radio" name="status" value="1" /> 禁用
            </td>
        </tr>
        <tr>
            <td class="tableleft">角色</td>
            <td>
            	<select id="role" name="role">

       			 </select>
        	</td>
        </tr>
        <tr>
            <td class="tableleft"></td>
            <td>
                <button id="add" type="button" onclick="validataAdd()" class="btn btn-primary" type="button">更新</button>&nbsp;&nbsp;<button type="button" class="btn btn-success" name="backid" id="backid">返回列表</button>
            </td>
        </tr>
    </table>
</form>
</body>
</html>